package com.sita.friend.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.ui.activity.roadtrust.NavigateActivity;
import com.sita.tboard.ActivityBase;

import java.util.ArrayList;
import java.util.List;

public class PoiDestinationSearchActivity extends ActivityBase {
    private PoiSearch.Query query;// Poi查询条件类
    private String deepType = "";// poi搜索类型
    private int currentPage = 0;// 当前页面，从0开始计数
    private LatLonPoint lp = new LatLonPoint(39.908127, 116.375257);// 默认西单广场
    private PoiSearch poiSearch;
    private PoiResult poiResult; // poi返回的结果
    private List<PoiItem> poiItems;// poi数据
    private List<String> poiitemdata = new ArrayList<>();
    private List<LatLonPoint> poiLatLonPointList = new ArrayList<>();
    private ListView poilst;
    private Button cancelSearch;
    private EditText searchDistination;
    private boolean isFromDashboard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poi_destination_search);
        isFromDashboard = getIntent().getBooleanExtra("MAP_SPEED_DASHBOARD_FRAGMENT",false);
        cancelSearch = (Button) findViewById(R.id.btn_cancel_search);
        searchDistination = (EditText) findViewById(R.id.edtTxt_search_destination);
        poilst = (ListView) findViewById(R.id.list_poi);
        AMapLocation location = GlobalContext.getLocationClient().getLastKnownLocation();
        final String city = location.getCity();
        final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.poi_search_listview_item, poiitemdata);
        poilst.setAdapter(adapter);
        poilst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView poi_search_listview_item = (TextView) view.findViewById(R.id.poi_search_listview_item);
                String poi_search_item_tv = poi_search_listview_item.getText().toString();
                LatLonPoint latLonPoint = poiLatLonPointList.get(position);
                Intent intent = null;
                if(isFromDashboard){
                    intent = new Intent(getApplicationContext(),NavigateActivity.class);
                }else{
                    intent = new Intent(getApplicationContext(), RoutePlanningActivity.class);
                }
                Bundle bundle = new Bundle();
                bundle.putString("myLocation", poi_search_item_tv);
                bundle.putParcelable("latLonPoint", latLonPoint);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
        cancelSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        searchDistination.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String location_key = searchDistination.getText().toString();
                query = new PoiSearch.Query(location_key, "", city);
                query.setPageSize(20);// 设置每页最多返回多少条poiitem
                query.setPageNum(currentPage);//设置查询页码
                poiSearch = new PoiSearch(getApplicationContext(), query);//初始化poiSearch对象
                poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult result, int rCode) {
                        if (rCode == 0) {
                            if (result != null && result.getQuery() != null) {// 搜索poi的结果
                                if (result.getQuery().equals(query)) {// 是否是同一条
                                    poiResult = result;
                                    poiItems = poiResult.getPois();// 取得第一页的poiitem数据，页数从数字0开始
                                    poiitemdata.clear();
                                    poiLatLonPointList.clear();
                                    for (int i = 0; i < poiItems.size(); i++) {
                                        PoiItem poiItem = poiItems.get(i);
                                        poiitemdata.add(poiItem.getTitle());
                                        poiLatLonPointList.add(poiItem.getLatLonPoint());
                                    }
                                    adapter.notifyDataSetChanged();
                                    List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys();// 当搜索不到poiitem数据时，会返回含有搜索关键字的城市信息
                                }
                            }
                        }
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {

                    }

                });//设置回调数据的监听器
                poiSearch.searchPOIAsyn();//开始搜索
            }
        });

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
